﻿using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using System.Xml.Linq;

namespace AspDotNet.Core6.WebApi.Controllers
{
    [ApiController]
    [ApiVersion("1.0")]
    [Produces("application/json")]
    [Route("api/[controller]/[action]")]
    public class FromController : ControllerBase
    {

        private IWebHostEnvironment webHostEnvironment;

        public FromController(IWebHostEnvironment environment)
        {
            webHostEnvironment = environment;
        }

        [HttpPost] public IActionResult GetPerson(People people) => Ok(people);
        [HttpPost] public IActionResult FromBody([FromBody] People people) => Ok(people);
        [HttpPost] public IActionResult FromQuery([FromQuery] People people) => Ok(people);
        [HttpPost] public IActionResult FromForm([FromForm] People people) => Ok(people);
        [HttpPost] public IActionResult FromForm1([FromForm] People people) => Ok(people);
        [HttpPost] public IActionResult FromHeader([FromHeader] string name, [FromHeader] string age) => Ok(new People() { age = age, name = name });
        [HttpPost("{name}")] public IActionResult FromRoute([FromRoute] string name, [FromQuery] string age) => Ok(new People() { age = age, name = name });

        [HttpGet("{id}")]
        public IActionResult Get(int id)
        {
            return Ok(id);
        }



        [HttpPost("UploadFile")]
        public async Task<string> UploadFile([FromForm] IFormFile file)
        {
            string path = Path.Combine(webHostEnvironment.WebRootPath, "Images/" + file.FileName);
            using (var stream = new FileStream(path, FileMode.Create))
            {
                await file.CopyToAsync(stream);
            }
            return "https://localhost:44324/Images/" + file.FileName;
        }

        [HttpPost("PostXml")]
        [Consumes("application/xml")]
        public object PostXml([FromBody] System.Xml.Linq.XElement res)
        {
            return res.ToString();
        }
    }

    public class People
    {
        public string name { get; set; }
        public string age { get; set; }
    }

}
